<template>
  <van-list
    v-model="loading"
    :finished="finished"
    finished-text="没有更多了"
    @load="onLoad"
  >
    <comment-item @click-reply="$emit('click-reply',$event)" v-for="item in list" :key="item.com_id+''" :item="item" />
  </van-list>
</template>

<script>
import { getComment } from '@/api/article.js'
import CommentItem from '@/components/CommentItem.vue'
export default {
  components: { CommentItem },
  props: {
    type: {
      type: String,
      default: 'a'
    },
    ID: {
      type: [String, Number, Object],
      required: true
    },
    list: {
      type: Array,
      default: () => []
    }
  },
  data () {
    return {
      loading: false,
      finished: false,
      offset: null,
      limit: 10
    }
  },

  created () {

  },

  watch: {

  },
  methods: {
    async onLoad () {
      try {
        const res = await getComment({
          type: this.type,
          source: this.ID + '',
          offset: this.offset,
          limit: this.limit
        })
        this.list.push(...res.results)
        this.$emit('onload', res.total_count)
        this.loading = false
        this.offset = res.last_id
        if (!res.results?.length) {
          this.finished = true
        }
      } catch (err) {
        console.log(err)
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
